package blog.service.impl;

import blog.dao.NoticeCategoryDao;
import blog.pojo.ChapterCategory;
import blog.pojo.NoticeCategory;
import blog.service.NoticeCategoryService;
import blog.util.Timestamp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class NoticeCategoryServiceImpl implements NoticeCategoryService {

    @Autowired
    private NoticeCategoryDao dao;

    @Override
    public Page<NoticeCategory> getPage(Page<NoticeCategory> page, NoticeCategory noticeCategory, String keywords) throws Exception {
        if (page == null) return null;
        QueryWrapper<NoticeCategory> queryWrapper = new QueryWrapper<>(noticeCategory)
                .ne("isuse", 2);
        if (keywords != null) {
            queryWrapper.like("name", keywords);
        }
        page = dao.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Boolean insert(NoticeCategory noticeCategory) throws Exception {
        noticeCategory.setGmtCreate(Timestamp.gen()).setGmtUpdate(Timestamp.gen());
        return dao.insert(noticeCategory) > 0;
    }

    @Override
    public Boolean update(NoticeCategory noticeCategory) throws Exception {
        noticeCategory.setGmtUpdate(Timestamp.gen());
        return dao.updateById(noticeCategory) > 0;
    }

    @Override
    public NoticeCategory get(NoticeCategory noticeCategory) throws Exception {
        return dao.selectOne(new QueryWrapper<>(noticeCategory));
    }

    @Override
    public List<NoticeCategory> getAll() throws Exception {
        return dao.selectList(null);
    }

    @Override
    public NoticeCategory selectById(Integer id) throws Exception {
        return dao.selectById(id);
    }
}
